package com.xteam.web.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.bean.BeanHelper;
import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.purview.entities.PurviewFunNode;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.service.impl.AuthenticationFace;
import com.xteam.purview.service.impl.PurviewFunNodeFace;
import com.xteam.utils.WebTools;

public class FunNodeController extends PurViewController implements IController {

	public FunNodeController() {
	}

	private PurviewFunNodeFace	face	= PurviewFunNodeFace.getInstance();
	private AuthenticationFace	aface	= AuthenticationFace.getInstance();
	
	@Autowired
	private SystemLogService logService;

	/**
	 * 查找用户有权限的功能节点
	 * @author tangkf ---2012-2-9
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView findUserFunNode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		// 获取session值
		Object uo = request.getSession().getAttribute(PurviewUser.PROP_USERID);
		PurviewUser user = (uo != null && uo instanceof PurviewUser) ? (PurviewUser) uo : null;
		String myfuns	= "{}";
		if (user != null) {
			System.err.println("SESSION 中的用户："+user);
			//Map<String,PurviewFunNode> funs	= aface.getFunNodeByUser(user.getUserId());
			JSONArray jsa	= aface.getFunNodeTreeByUser(user.getUserId());
			myfuns	=jsa.toString();// JSONArray.fromObject(funs).toString();//
		}
		PrintWriter out = response.getWriter();
		out.print(myfuns);
		out.close();
		return null;
	}

	/**
	 * 查找全部功能节点
	 * @see cn.mystic.web.former.PurViewController#find(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView find(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.find(request, response);
		PrintWriter out = response.getWriter();
		String data = face.getFunNodeJsonTreeNode().toString();
		out.print(data);
		out.close();
		return null;
	}
	
	/**
	 * 查找全部功能节点
	 * @see cn.mystic.web.former.PurViewController#find(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView findNodeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.find(request, response);
		PrintWriter out = response.getWriter();
		List<PurviewFunNode> data = face.getAllFunNodes();
		JSONArray jsa	= JSONArray.fromObject(data);
		out.print(jsa.toString());
		out.close();
		return null;
	}

	public ModelAndView findByPid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);

		PrintWriter out = response.getWriter();
		String id = ReqHelper.getRequestString(request, "funId");
		String data = face.getFunNodeJsonByPid(id).toString();
		out.print(data);
		out.close();
		return null;
	}

	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.save(request, response);
		
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		String lslMethod = "save";

		Map<String, Object> _msg = new HashMap<String, Object>();
		PurviewFunNode fn = new PurviewFunNode();
		BeanHelper.requestToBean(fn, request);
		int r = 1;
		if (CommonUtils.isEmpty(fn.getFunId())) {
			r = face.save(fn);
		} else {
			r = face.update(fn);
			lslType = ConstantLocation.LOG_MODIFY;
			lslMethod = "update";
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_FUNNODE_MGR, 
				lslMethod, lslType, "PurviewFunNode");
		
		if (r > 0) {
			_msg.put("state", false);
			_msg.put("msg", "操作失败！");
		} else {
			_msg.put("state", true);
			_msg.put("msg", "操作成功！");
		}
		WebTools.writerJsonToResponse(response, _msg);
		return null;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.delete(request, response);
		
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		Map<String, Object> _msg = new HashMap<String, Object>();
		String id = ReqHelper.getRequestString(request, "funId");
		
		if (face.delBatch(PurviewFunNode.class, PurviewFunNode.PROP_FUNID, new String[] { id }) > 0) {
			_msg.put("state", true);
			_msg.put("msg", "操作成功！");
		} else {
			_msg.put("state", false);
			_msg.put("msg", "操作失败！");
		}
		
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_FUNNODE_MGR, 
				"delBatch", ConstantLocation.LOG_DELETE_RES, "PurviewFunNode");
		
		WebTools.writerJsonToResponse(response, _msg);
		return null;
	}

}
